﻿<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;

public class Upload : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (!context.User.Identity.IsAuthenticated)
        {
            context.Response.Write("Error");
            return;
        }
        string folder = context.Request["fld"];
        if (folder == null)
            folder = "";
        string strResponse = "error";
        if (context.Request.Files.Count > 0)
        {
            System.Collections.Generic.List<string> lsFiles = new System.Collections.Generic.List<string>();
            string path = context.Server.MapPath(SERP.Modules.ModuleBase.GetUserFilesStore() + "/" + folder);
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                string fileName = System.IO.Path.Combine(path, context.Request.Files[i].FileName);
                context.Request.Files[i].SaveAs(fileName);
                lsFiles.Add(fileName);
            }
            //Process files
            string srcFolder = context.Server.MapPath(SERP.Modules.ModuleBase.GetUserFilesStore() + "/" + folder);
            string destFolder = System.IO.Path.Combine(srcFolder, "decp");
            if (System.IO.Directory.Exists(destFolder))
            {
                if (destFolder.ToLower().IndexOf(HttpContext.Current.Server.MapPath(SERP.Modules.ModuleBase.GetUserFilesStore()).ToLower()) == 0)
                {
                    DeleteRescusive(destFolder);
                }
            }
            if (!System.IO.Directory.Exists(srcFolder))
                System.IO.Directory.CreateDirectory(srcFolder);
            System.Collections.Generic.List<string> lsAtrax = new System.Collections.Generic.List<string>();
            foreach (string st in lsFiles)
            {
                string ext = System.IO.Path.GetExtension(st).ToLower();
                switch (ext)
                {
                    case ".rar":
                    case ".zip":
                    case ".gz":
                    case ".bz":
                        System.Collections.Generic.List<string> lsTmp = SERP.Commons.CompressHelper.Read(st, destFolder);
                        foreach (string f in lsTmp)
                        {
                            lsAtrax.Add("decp\\" + f);
                        }
                        break;
                    default:
                        lsAtrax.Add(st.Substring(srcFolder.Length));
                        break;
                }
            }
            strResponse = string.Join("$", lsAtrax.ToArray());
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write(strResponse);
    }
    private void DeleteRescusive(string folder)
    {

        string[] files = System.IO.Directory.GetFiles(folder);
        foreach (string st in files)
        {
            System.IO.File.Delete(st);
        }
        files = System.IO.Directory.GetDirectories(folder);
        foreach (string st in files)
        {
            DeleteRescusive(st);
        }
        System.IO.Directory.Delete(folder, true);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}